package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaolong
 * @version 1.0
 * @description: TODO
 * @date 2021/9/17 9:20
 */
@RestController
@Slf4j
//@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")  //全局定义服务降级方法
public class OrderHystirxController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;


    @GetMapping("/consumer/payment/hystrix/ok/{id}")
//   全局@HystrixCommand
    public String pamentInfo_ok1(@PathVariable("id") Integer id) {
//        int age =10/0;
        String result = paymentHystrixService.pamentInfo_ok1(id);
        return result;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
//    特定服务降级，HystrixProperty条件（大于超时3s）满足则进入fallbackMethod这个方法，进行服务降级调用
//    @HystrixCommand(fallbackMethod = "pamentInfo_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    public String pamentInfo_TimeOut1(@PathVariable("id") Integer id) {
//        int age =10/0;
        String result = paymentHystrixService.pamentInfo_TimeOut1(id);
        return result;
    }


    public String pamentInfo_TimeOutHandler(Integer id) {
        return "我的消费者80，对方支付系统繁忙或者运行报错，请稍后再试";
    }


    //全局fallback方法
    public String payment_Global_FallbackMethod() {

        return "Global异常处理信息，请稍后再试";
    }

}
